package com.google.code.greenplate;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TimePicker;

public class GetARide extends Activity {
	private Button mGetarideButton;
	private TextView mTimeLeave;
	private ImageButton mSetTime;
	private EditText mDestination;
		
	static final int TIME_DIALOG_ID = 0;
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.getaride);
        setWidgetReferences();
        addWidgetListeners();
        
        // Get the current time
        final Calendar c = Calendar.getInstance();
                
        // Display the current date
        updateDisplay(c.getTime());
    }
	
    private void setWidgetReferences() {
    	mGetarideButton = (Button) findViewById(R.id.getarideButton);
    	mTimeLeave = (TextView) findViewById(R.id.timeLeave);
    	mSetTime = (ImageButton) findViewById(R.id.setTime);
    	mDestination = (EditText) findViewById(R.id.destination);
    }
       
	private class GetarideButtonListener implements OnClickListener {
		public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(GetARide.this, LiveRides.class);
            startActivity(intent);
		}
	}
	
	private class SetTimeListener implements OnClickListener {
		public void onClick(View v) {
			showDialog(TIME_DIALOG_ID);
		}
	}
    
    private void addWidgetListeners() {
    	mGetarideButton.setOnClickListener(new GetarideButtonListener());    	
    	mSetTime.setOnClickListener(new SetTimeListener());    	
    }
    
    // Updates the time displayed in the TextView
    private void updateDisplay(Date date) {    	
    	Format formatter = new SimpleDateFormat("hh:mm aaa");    	
    	mTimeLeave.setText(formatter.format(date));
    }
    
    // The callback received when user sets the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
    	new TimePickerDialog.OnTimeSetListener() {			
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			
		        final Calendar c = Calendar.getInstance();
		        c.set(Calendar.HOUR_OF_DAY, hourOfDay);
		        c.set(Calendar.MINUTE, minute);		        
				updateDisplay(c.getTime());
			}
		};
		
	@Override
	protected Dialog onCreateDialog(int id) {
		final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
		switch (id) {
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, mTimeSetListener, hour, minute, false);
		}
		return null;
	}
}